#include "simplemainwindow.h"
#include "simplegraphicbutton.h"
#include <QVBoxLayout>
#include <QHBoxLayout>
#include <QGridLayout>
#include <QSqlQuery>
#include <QVariant>

SimpleMainWindow::SimpleMainWindow(QWidget *parent) :
    QDialog(parent)
{
    resize(800,480);

    //主界面整体布局管理
    QVBoxLayout *mainlayout = new QVBoxLayout;
    QGridLayout *layout = new QGridLayout;
    for (int i=0; i<NUM_OF_BTN; ++i)
    {
        gra_button[i] = new SimpleGraphicButton(this);
        layout->addWidget(gra_button[i], i/BTN_COL, i%BTN_COL);
    }
    mainlayout->addLayout(layout);
    setLayout(mainlayout);

    create_initial_show();
}


void SimpleMainWindow::create_initial_show()
{
    btn_of_current_page.eraseAllRecord();
    QSqlDatabase::database().transaction();
    QSqlQuery query;
    query.exec("SELECT * FROM locate_information_table");
    for(int i = 0; i<NUM_OF_BTN && query.next()==true; ++i)
    {
        gra_button[i]->init_const_member();
        gra_button[i]->set_start_led(TURN_ON);
        gra_button[i]->set_fault_led(TURN_ON);
        gra_button[i]->set_fedback_led(TURN_ON);

        QString num = query.value(0).toString();
        QString dev_name = query.value(5).toString();
        QString locate_inf = dev_name +tr("\n")+/* query.value(1).toString() + query.value(2).toString()+*/
                             query.value(3).toString()+query.value(4).toString();

        gra_button[i]->set_geo_description_and_num(locate_inf);
        btn_of_current_page.addOneRecord(num.toInt(), TAIL,SysMode::SIMPLE_MODE);
    }
    QSqlDatabase::database().commit();
}


void SimpleMainWindow::s_goto_previous_page()
{
    if(SysMode::Instance()->getMode()!=SysMode::SIMPLE_MODE)
    {
        return;
    }

    if(btn_of_current_page.isFistPage())
    {
        qDebug()<<"This is already the first page!";
        return;
    }
    btn_of_current_page.setToLastPage(false);
    int current_min_number = btn_of_current_page.getMinID();
    btn_of_current_page.eraseAllRecord();
    qDebug()<<"int current_min_number = btn_of_current_page.getMinID()  "<<current_min_number;
    QSqlQuery query;
    query.exec(QString("SELECT * FROM locate_information_table where id < %1 order by id desc LIMIT %2").arg(current_min_number).arg(NUM_OF_BTN));

    int cnt = 0;
    QString num,dev_name,locate_inf;
    for( cnt = NUM_OF_BTN - 1;  cnt>=0 && query.next()==true;  --cnt )
    {
        gra_button[cnt]->init_const_member();
        gra_button[cnt]->set_start_led(TURN_ON);
        gra_button[cnt]->set_fault_led(TURN_ON);
        gra_button[cnt]->set_fedback_led(TURN_ON);
        num = query.value(0).toString();
        dev_name = query.value(5).toString();
        locate_inf = dev_name +tr("\n")/*+ query.value(1).toString() + query.value(2).toString()*/
                     +query.value(3).toString()+query.value(4).toString();
        gra_button[cnt]->set_geo_description_and_num(locate_inf);

        //add a record to the btn_of_current_page table
        btn_of_current_page.addOneRecord(num.toInt(),HEAD,SysMode::SIMPLE_MODE);
    }

    //this meas it has come to the first page
    if( cnt >= 0 )
    {
        btn_of_current_page.eraseAllRecord();
        query.exec(QString("SELECT * FROM locate_information_table order by id LIMIT %1").arg(NUM_OF_BTN));
        for( cnt= 0; cnt<NUM_OF_BTN && query.next()==true; ++cnt )
        {
            gra_button[cnt]->init_const_member();
            gra_button[cnt]->set_start_led(TURN_ON);
            gra_button[cnt]->set_fault_led(TURN_ON);
            gra_button[cnt]->set_fedback_led(TURN_ON);
            num = query.value(0).toString();
            dev_name = query.value(5).toString();
            locate_inf = dev_name +tr("\n")/*+ query.value(1).toString() + query.value(2).toString()*/
                         +query.value(3).toString()+query.value(4).toString();
            gra_button[cnt]->set_geo_description_and_num(locate_inf);

            //add a record to the btn_of_current_page table
            btn_of_current_page.addOneRecord(num.toInt(),TAIL,SysMode::SIMPLE_MODE);
        }
        btn_of_current_page.setToFisrtPage(true);
    }
}


void SimpleMainWindow::s_goto_next_page()
{
    if(SysMode::Instance()->getMode()!=SysMode::SIMPLE_MODE)
    {
        return;
    }

    if( btn_of_current_page.isLastPage())
    {
        qDebug()<<"This is already the last page!";
        return;
    }

    int current_max_number = btn_of_current_page.getMaxID();
    if(current_max_number == INVALID_MAX_ID)
        return;
    btn_of_current_page.setToFisrtPage(false);

#if DEBUG_ON
    qDebug()<<"int current_max_number = btn_of_current_page.getMaxID()  "<<current_max_number;
#endif

    QSqlQuery query;
    query.exec(QString("SELECT * FROM locate_information_table where id > %1 ").arg(current_max_number));

    bool erase_falg=true;
    int cnt = 0;
    for( cnt= 0; cnt<NUM_OF_BTN && query.next()==true; ++cnt )
    {
        if(erase_falg)
        {
            erase_falg = false;
            btn_of_current_page.eraseAllRecord();
        }
        gra_button[cnt]->init_const_member();
        gra_button[cnt]->set_start_led(TURN_ON);
        gra_button[cnt]->set_fault_led(TURN_ON);
        gra_button[cnt]->set_fedback_led(TURN_ON);

        QString num = query.value(0).toString();
        QString dev_name = query.value(5).toString();
        QString locate_inf = dev_name +tr("\n")/*+ query.value(1).toString() + query.value(2).toString()*/
                     +query.value(3).toString()+query.value(4).toString();

        gra_button[cnt]->set_geo_description_and_num(locate_inf);

        //add a record to the btn_of_current_page table
        btn_of_current_page.addOneRecord(num.toInt(),TAIL,SysMode::SIMPLE_MODE);
    }

    if( cnt == 0 )
    {
        btn_of_current_page.setToLastPage(true);
        qDebug()<<"This is already the last page!";
        return;
    }

    if( cnt >0 && cnt < NUM_OF_BTN)
    {
        while( cnt<NUM_OF_BTN)
        {
            gra_button[cnt]->set_undefined_state();
            cnt++;
        }
        btn_of_current_page.setToLastPage(true);
    }
}

